$(function(){
	var admin_id = getCookie('admin_id');
	if(!admin_id){
		window.location.replace('login.html');
	}
	var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });
    //业务开始
    var id = getQueryString('id');

    if(!id){
    	swal("操作错误", "参数传递错误！", "error");
    	setTimeout(function(){
             location.href="goods_list.html";
		},3000);
		return false;
    }
    //获取列表
    $.ajax({
        //url: ApiUrl + "/goods/category",
        url: ApiUrl + "/goods/category/list?limit=10000&page=1",
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
            if(result.code == 200){
                var html='<option value="0">---请选择---</option>';
                $.each(result.data.list, function(k, v) {
                    html+='<option value="'+v.id+'">'+v.name+'</option>';
                });
                $("#category_id").html(html);
            }else{
                parent.layer.msg(result.message);
                return false;
            }
        }
    });
    //获取套餐列表
    $.ajax({
        url: ApiUrl + "/goods/taocang/list",
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
            if(result.code == 200){
                var html='<option value="0">---请选择---</option>';
                $.each(result.data, function(k, v) {
                    html+='<option value="'+v.id+'">'+v.title+'</option>';
                });
                $("#taocang_id").html(html);

            }else{
                parent.layer.msg(result.message);
                return false;
            }
        }
    });
    //获取基本信息
    $.ajax({
		url: ApiUrl + "/goods/"+id,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		document.getElementById("article_no").value = result.data.article_no;
        		document.getElementById("bar_code").value = result.data.bar_code;
        		document.getElementById("zh_name").value = result.data.zh_name;
        		document.getElementById("en_name").value = result.data.en_name;
        		document.getElementById("mnemonic").value = result.data.mnemonic;
        		document.getElementById("brand").value = result.data.brand;
        		document.getElementById("tax").value = result.data.tax;
        		document.getElementById("price").value = result.data.price;
        		document.getElementById("inventory").value = result.data.inventory;

        		if(result.data.status == 1){
        			$("#site_status1").attr("checked","checked");
					$("#site_status0").removeAttr("checked");
					$(".cb-enable").addClass('selected');
  					$(".cb-disable").removeClass('selected');
        		}else{
        			$("#site_status0").attr("checked","checked");
					$("#site_status1").removeAttr("checked");
					$(".cb-enable").removeClass('selected');
  					$(".cb-disable").addClass('selected');
        		}
        		$("#category_id").val(result.data.category_id);
        		$("#taocang_id").val(result.data.taocang_id);
        	}else{
        		parent.layer.msg(result.message);
        		return false;
        	}
        }
   });
	//自定义radio样式
	$(".cb-enable").click(function(){
	    var parent = $(this).parents('.onoff');
	    $('.cb-disable',parent).removeClass('selected');
	    $(this).addClass('selected');
	    $('.checkbox',parent).attr('checked', true);
	});
	$(".cb-disable").click(function(){
	    var parent = $(this).parents('.onoff');
	    $('.cb-enable',parent).removeClass('selected');
	    $(this).addClass('selected');
	    $('.checkbox',parent).attr('checked', false);
	});
	//提交表单
   $("#submit").click(function(){
   		 if($("#signupForm").valid()){
          var query = $("#signupForm").serialize();
          ajax_post(ApiUrl + "/goods/"+id,"goods_list.html",query);
    	}
   });
   var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            article_no: {
                required: true
            },
            bar_code: {
                required: true
            },
            zh_name: {
                required: true
            },
            en_name: {
                required: true
            },
            mnemonic: {
                required: true
            },
            brand: {
                required: true
            },
            tax: {
                required: true,
                number:true
            },
             price: {
                required: true,
                number:true
            },
             inventory: {
                required: true,
                number:true
            }
        },
        messages: {
            article_no: {
                required: icon+ "请输入商品货号！"
            },
            bar_code: {
                required: icon+ "请输入商品条码！"
            },
            zh_name: {
                required: icon+ "请输入商品名称（中文）！"
            },
            en_name: {
                required: icon+ "请输入商品名称（英文）！"
            },
            mnemonic: {
                required: icon+ "请输入商品助记号！"
            },
            brand: {
                required: icon+ "请输入商品品牌！"
            },
            tax: {
                required: icon+ "请输入商品税额！",
                number :  icon+"商品税额只能是数字" 
            },
            price: {
                required: icon+ "请输入商品价格！",
                 number :  icon+"商品价格只能是数字" 
            },
            inventory: {
                required: icon+ "请输入商品库存！",
                number :  icon+"商品库存只能是数字" 
            }
        }
    });
});